package com.ihomey.linkuphome.adapter;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ihomey.linkuphome.R;
import com.ihomey.linkuphome.entities.SingleDevice;
import com.ihomey.linkuphome.listeners.OnItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.List;

/**
 * Created by Administrator on 2016/12/28.
 */

public class DeviceListAdapter extends SwipeMenuAdapter<DeviceListAdapter.ListHolder> {
    public static final int TYPE_NORMAL = 2;
    //获取从Activity中传递过来每个item的数据集合
    private List<SingleDevice> mDatas;
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    //构造函数
    public DeviceListAdapter(List<SingleDevice> list) {
        this.mDatas = list;
    }

    @Override
    public int getItemViewType(int position) {
        return TYPE_NORMAL;
    }

    private Drawable getDrawable(TextView button, int resId) {
        Drawable drawable = button.getContext().getResources().getDrawable(resId);
        drawable.setBounds(button.getCompoundDrawables()[2].getBounds());
        return drawable;
    }


    @Override
    public void onBindViewHolder(ListHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_NORMAL) {
            SingleDevice singleDevice = mDatas.get(position);
            holder.nameText.setText(singleDevice.getName());
            if (singleDevice.getDeviceId() != 0) {
                holder.stateText.setText(R.string.state_connected);
                holder.stateText.setCompoundDrawables(null, null, getDrawable(holder.stateText, R.drawable.ic_device_connect), null);
            } else {
                holder.stateText.setText(R.string.state_disconnected);
                holder.stateText.setCompoundDrawables(null, null, getDrawable(holder.stateText, R.drawable.ic_device_disconnect), null);
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(position);
                    }
                }
            });
            holder.stateText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if (mOnItemClickListener != null) {
//                        mOnItemClickListener.onDeviceConnect(position);
//                    }
                }
            });
            return;
        } else {
            return;
        }
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_device, parent, false);
        return layout;
    }

    @Override
    public ListHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new ListHolder(realContentView);
    }

    //在这里面加载ListView中的每个item的布局
    class ListHolder extends RecyclerView.ViewHolder {
        TextView nameText;
        TextView stateText;

        public ListHolder(View itemView) {
            super(itemView);
            nameText = (TextView) itemView.findViewById(R.id.tv_device_name);
            stateText = (TextView) itemView.findViewById(R.id.tv_device_state);
        }
    }

    //返回View中Item的个数，这个时候，总的个数应该是ListView中Item的个数加上HeaderView和FooterView
    @Override
    public int getItemCount() {
        return mDatas.size();
    }

}
